# game_defs.py


# 棋盘尺寸
BOARD_SIZE = 7

# 棋盘初始化
board = [[' ' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
board[0] = ['@', '-', '-', '@', '-', '-', '@']
board[1] = ['|', '@', '-', '@', '-', '@', '|']
board[2] = ['|', '|', '@', '@', '@', '|', '|']
board[3] = ['@', '@', '@', ' ', '@', '@', '@']
board[4] = ['|', '|', '@', '@', '@', '|', '|']
board[5] = ['|', '@', '-', '@', '-', '@', '|']
board[6] = ['@', '-', '-', '@', '-', '-', '@']

# 第二个棋盘初始化
board2 = [[' ' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
board2[0] = ['@', '-', '-', '@', '-', '-', '@']
board2[1] = ['|', '@', '-', '@', '-', '@', '|']
board2[2] = ['|', '|', '@', '@', '@', '|', '|']
board2[3] = ['@', '@', '@', ' ', '@', '@', '@']
board2[4] = ['|', '|', '@', '@', '@', '|', '|']
board2[5] = ['|', '@', '-', '@', '-', '@', '|']
board2[6] = ['@', '-', '-', '@', '-', '-', '@']

# 输出当前棋盘状态
def print_board():
    print('  ' + ' '.join([chr(ord('A') + i) for i in range(BOARD_SIZE)]) + '   ' + ' '.join([chr(ord('A') + i + BOARD_SIZE) for i in range(BOARD_SIZE)]))
    for i in range(BOARD_SIZE):
        print(str(i + 1) + ' ' + ' '.join(board[i]) + '   ' + ' '.join(board2[i]))


# 落子
def place_piece(row, col, piece):
    boardptr = board2 if col >= BOARD_SIZE else board
    col_adjusted = col - BOARD_SIZE if col >= BOARD_SIZE else col

    if row < 0 or row >= BOARD_SIZE or col_adjusted < 0 or col_adjusted >= BOARD_SIZE:
        raise ValueError('无效的落子位置')
    if boardptr[row][col_adjusted] != '@':
        raise ValueError('该位置已有棋子')
    boardptr[row][col_adjusted] = piece
    return True

# 移动棋子
def move_piece(src_row, src_col, dst_row, dst_col):
    src_board = board2 if src_col >= BOARD_SIZE else board
    dst_board = board2 if dst_col >= BOARD_SIZE else board
    src_col_adjusted = src_col - BOARD_SIZE if src_col >= BOARD_SIZE else src_col
    dst_col_adjusted = dst_col - BOARD_SIZE if dst_col >= BOARD_SIZE else dst_col

    if src_board is not dst_board:
        raise ValueError('不能跨棋盘移动棋子')
    
    if src_row < 0 or src_row >= BOARD_SIZE or src_col_adjusted < 0 or src_col_adjusted >= BOARD_SIZE:
        return False
    if dst_row < 0 or dst_row >= BOARD_SIZE or dst_col_adjusted < 0 or dst_col_adjusted >= BOARD_SIZE:
        return False
    if src_board[src_row][src_col_adjusted] == '@' or dst_board[dst_row][dst_col_adjusted] != '@':
        return False
    if src_board[src_row][src_col_adjusted] != 'X' and src_board[src_row][src_col_adjusted] != 'O':
        return False
    dst_board[dst_row][dst_col_adjusted] = src_board[src_row][src_col_adjusted]
    src_board[src_row][src_col_adjusted] = '@'
    return True

# 吃子
def capture_piece(row, col):
    boardptr = board2 if col >= BOARD_SIZE else board
    col_adjusted = col - BOARD_SIZE if col >= BOARD_SIZE else col

    if row < 0 or row >= BOARD_SIZE or col_adjusted < 0 or col_adjusted >= BOARD_SIZE:
        return False
    if boardptr[row][col_adjusted] == ' ':
        return False
    boardptr[row][col_adjusted] = '@'
    return True
